package com.uenpay.im.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qyx.android.entity.FriendEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class UserListAdatper extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<FriendEntity> list;

	public UserListAdatper(Context context, ArrayList<FriendEntity> _list) {
		mInflater = LayoutInflater.from(context);
		list = _list;
	}

	public void addData(ArrayList<FriendEntity> _list) {
		list.addAll(_list);
		notifyDataSetChanged();
	}

	public void setData(ArrayList<FriendEntity> _list) {
		list = _list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list == null ? null : list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		UserViewHodler holder = null;
		FriendEntity user = list.get(arg0);
		if (arg1 == null) {
			holder = new UserViewHodler();
			arg1 = mInflater.inflate(R.layout.user_list_item_layout, arg2,false);
			holder.avatar = (RoundImageView) arg1.findViewById(R.id.user_avatar);
			holder.name = (TextView) arg1.findViewById(R.id.user_name);
			arg1.setTag(holder);
		} else {
			holder = (UserViewHodler) arg1.getTag();
		}
		String url = APIConfiguration.getAvatarUrlNormal(user.cust_id,
				PacketType.PERSONAL);
		holder.avatar.SetUrl(url);
		
		if(!TextUtils.isEmpty(user.remarks_name)){
			holder.name.setText(user.remarks_name);
		}else{
			holder.name.setText(user.nick_name);
		}
		return arg1;
	}

	class UserViewHodler {
		private RoundImageView avatar;
		private TextView name;
	}

}
